# Maintainer: Filipe Laíns (FFY00) <filipe.lains@gmail.com>

pkgbase=libad9361
pkgname=(libad9361 libad9361-docs)
_name=$pkgbase-iio
pkgver=0.2
pkgrel=8
pkgdesc='IIO AD9361 library for filter design and handling, multi-chip sync, etc.'
arch=(loong64 x86_64)
url="https://github.com/analogdevicesinc/libad9361-iio"
license=(LGPL2.1)
makedepends=(cmake doxygen graphviz libiio)
source=($url/archive/v$pkgver/$_name-v$pkgver.tar.gz)
sha512sums=('f95fa955531198ab33c16d41685d23140e875b2eafa1da10cdf60a7bf1ae760e86e2d3d28c9ab36a3a7b7a90e36bb53456dec1d6e537a1574a5505b7595e764c')
b2sums=('9880114bc53f3d4975dbe0773f8d112e40b0df47e34985bdc68c5b6740aee2c140ced39353929a793c2b327b5811461436ceb2934c2a71fc7b92b218092d468d')

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

build() {
  local cmake_options=(
    -B build
    -D CMAKE_BUILD_TYPE=None
    -D CMAKE_INSTALL_PREFIX=/usr
    -D ENABLE_PACKAGING=OFF
    -S $_name-$pkgver
    -W no-dev
  )

  cmake "${cmake_options[@]}"
  cmake --build build --verbose
}

package_libad9361() {
  depends=(glibc libiio)
  optdepends=(
    'libad9361-docs: for documentation'
  )

  DESTDIR="$pkgdir" cmake --install build

  (
    cd "$pkgdir"
    _pick $pkgbase-docs usr/share/doc
  )
}

package_libad9361-docs() {
  pkgdesc+=" - documentation"

  mv -v $pkgname/* "$pkgdir"
}
